{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 逆回购，资金宽松（央行发钱）。央行从商业银行或金融机构手中购买债券，同时约定在未来某一日期以稍高价格将债券卖回。\n",
    "- 正回购，资金紧张（央行收钱）。央行将债券卖给商业银行或金融机构，同时约定在未来某一日期以稍低价格买回债券。\n",
    "\n",
    "\n",
    " 两者的对比\n",
    "|项目\t|逆回购|\t正回购|\n",
    "|:--|:--|:--|\n",
    "|资金流动方向|\t央行向市场投放资金|\t央行从市场回笼资金|\n",
    "|市场效果|\t（净投放）市场流动性增加，利率下降|\t（净回笼）市场流动性减少，利率上升|\n",
    "|央行操作|\t买入债券，将资金注入市场|\t卖出债券，从市场回笼资金|\n",
    "|使用场景|\t市场资金紧张或利率偏高|\t市场资金宽裕或利率偏低|\n",
    "\n",
    "[逆回购查询](http://www.pbc.gov.cn/zhengcehuobisi/125207/125213/125431/125475/index.html)\n",
    "\n",
    "\n",
    "净投放数据涉及到央行的公开市场操作，包括逆回购、正回购、MLF（中期借贷便利）以及国债等到期情况，通常计算方式如下：\n",
    "\n",
    "净投放数据的计算：\n",
    "净投放量 = 总投放量 − 总回笼量\n",
    "\n",
    "总投放量：包括当日逆回购金额、MLF操作金额等。\n",
    "总回笼量：包括当日正回购金额、到期逆回购金额、到期MLF金额等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'akshare' has no attribute 'macro_china_market_open'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 5\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 获取央行近5个交易日公开市场操作数据\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[43mak\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmacro_china_market_open\u001b[49m()\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# 筛选最近5个交易日的数据\u001b[39;00m\n\u001b[0;32m      8\u001b[0m recent_data \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39mhead(\u001b[38;5;241m5\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'akshare' has no attribute 'macro_china_market_open'"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 获取央行近5个交易日公开市场操作数据\n",
    "data = ak.macro_china_market_open()\n",
    "\n",
    "# 筛选最近5个交易日的数据\n",
    "recent_data = data.head(5)\n",
    "\n",
    "# 提取日期和主要操作数据\n",
    "dates = recent_data['日期']\n",
    "reverse_repo = recent_data['逆回购操作']  # 逆回购操作量\n",
    "repo = recent_data['正回购操作']  # 正回购操作量\n",
    "\n",
    "# 绘制曲线图\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(dates, reverse_repo, label='逆回购操作量', marker='o', color='blue')\n",
    "plt.plot(dates, repo, label='正回购操作量', marker='o', color='orange')\n",
    "plt.title('央行近5个交易日公开市场操作', fontsize=16)\n",
    "plt.xlabel('日期', fontsize=12)\n",
    "plt.ylabel('操作量（亿元）', fontsize=12)\n",
    "plt.xticks(rotation=45)\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
